package com.xindz.hgq.database.greendao.view;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.xindz.hgq.R;
import com.xindz.hgq.adapter.LZTXShowAdapter;
import com.xindz.hgq.app.App;
import com.xindz.hgq.base.BaseTitleActivity;
import com.xindz.hgq.base.options.BaseActivityOptions;
import com.xindz.hgq.database.greendao.bean.DaoSession;
import com.xindz.hgq.database.greendao.bean.LztxBean;
import com.xindz.hgq.database.greendao.bean.LztxBeanDao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.database.greendao.view
 * @describe describe
 * @anthor XINDZ
 * @time 2018/5/31 16:25
 * @change
 * @chang time
 * @class describe
 */
public class GdV2Activity extends BaseTitleActivity {
    @BindView(R.id.bt_gd_v2_se_insert)
    Button btgd_v2SeInsert;
    @BindView(R.id.bt_gd_v2_se_delete_all)
    Button btgd_v2SeDeleteAll;
    @BindView(R.id.bt_gd_v2_se_query)
    Button btgd_v2SeQuery;
    @BindView(R.id.bt_gd_v2_se_query_all)
    Button btgd_v2SeQueryAll;
    @BindView(R.id.rcv_gd_v2_se)
    RecyclerView rcvgd_v2Se;
    @BindView(R.id.et_gd_v2_input_key)
    EditText etGdV2InputKey;
    @BindView(R.id.bt_gd_v2_se_delete)
    Button btGdV2SeDelete;

    private List<LztxBean> dataStringList;
    private LZTXShowAdapter adapter;

    private int num = 0;
    private DaoSession daoSession;
    private LztxBeanDao lztxBeanDao;
    private String objId = "LZTX";


    @Override
    public BaseActivityOptions setOption() {
        return new BaseActivityOptions().openTitle();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        RequestManager with = Glide.with(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gd_v2);
        ButterKnife.bind(this);
        dataStringList = new ArrayList<>();
        adapter = new LZTXShowAdapter(R.layout.item_gd_v2, dataStringList);
        rcvgd_v2Se.setLayoutManager(new LinearLayoutManager(this));
        rcvgd_v2Se.setAdapter(adapter);
        daoSession = App.app.getDaoSession();
        lztxBeanDao = daoSession.getLztxBeanDao();
    }

    @OnClick({R.id.bt_gd_v2_se_insert, R.id.bt_gd_v2_se_delete, R.id.bt_gd_v2_se_delete_all, R.id.bt_gd_v2_se_query, R.id.bt_gd_v2_se_query_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_gd_v2_se_insert:
                LztxBean lztxBean = createBean();
                daoSession.runInTx(new Runnable() {
                    @Override
                    public void run() {
                        long insertResult = lztxBeanDao.insertOrReplace(lztxBean);
                        if (-1 != insertResult) {
                            ToastUtils.showShort("添加成功");
                        } else {
                            ToastUtils.showShort("添加失败");
                        }
                    }
                });

                break;
            case R.id.bt_gd_v2_se_delete:
                lztxBeanDao.deleteByKey(objId);

                break;
            case R.id.bt_gd_v2_se_delete_all:
                daoSession.runInTx(new Runnable() {
                    @Override
                    public void run() {
                        lztxBeanDao.deleteAll();
                        ToastUtils.showShort("删除所有数据");
                    }
                });
                break;
            case R.id.bt_gd_v2_se_query:
                List<LztxBean> list = lztxBeanDao.queryBuilder().where(LztxBeanDao.Properties.ObjectId.eq(objId)).list();
                dataStringList.clear();
                dataStringList.addAll(list);
                adapter.notifyDataSetChanged();
                break;
            case R.id.bt_gd_v2_se_query_all:
                List<LztxBean> lztxBeans = lztxBeanDao.loadAll();
                dataStringList.clear();
                dataStringList.addAll(lztxBeans);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    public LztxBean createBean() {
        num++;
        long timeMillis = System.currentTimeMillis();
        Date dateByNow = TimeUtils.getDateByNow(timeMillis, TimeConstants.MSEC);
        String inputStr = etGdV2InputKey.getEditableText().toString().trim();
        if ("".equals(inputStr)) {
            objId = "LZTX" + timeMillis;
        } else {
            objId = inputStr + timeMillis;
        }

        Byte abyte = Byte.MAX_VALUE;
        Integer aInteger = Integer.MAX_VALUE;
        Short ashort = Short.MAX_VALUE;
        Long along = Long.MAX_VALUE;
        Boolean abooble = Boolean.TRUE;
        Double aDouble = Double.MAX_VALUE;
        Float afloat = Float.MAX_VALUE;
        Drawable draw_bg_1 = getResources().getDrawable(R.drawable.bg_1);
        Drawable draw_bg_2 = getResources().getDrawable(R.drawable.bg_2);


        Bitmap bitmap_bg_2 = ConvertUtils.drawable2Bitmap(draw_bg_2);
//Drawable 转 byte[]
//        byte[] bytes_bg_1 = ConvertUtils.drawable2Bytes(draw_bg_1, Bitmap.CompressFormat.JPEG);
//Bitmap 转 byte[]
        byte[] bytes_bg_2 = ConvertUtils.bitmap2Bytes(bitmap_bg_2, Bitmap.CompressFormat.JPEG);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("List 第一条");
        arrayList.add("List 第二条");

        return new LztxBean(objId, abyte, aInteger, ashort, along, abooble, aDouble, afloat, dateByNow, bytes_bg_2, arrayList);
    }

}
